package com.zzyl.serve.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.service.INursingProjectService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 护理项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-26
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper,NursingProject> implements INursingProjectService
{


    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return this.getById(id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return getBaseMapper().selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        //清理缓存
        removeCache();
        return this.save(nursingProject)? 1:0;
    }

    /**
     * 修改护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        //清理缓存
        removeCache();
        return this.updateById(nursingProject)?1:0;
    }

    /**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        //清理缓存
        removeCache();
        return this.removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        //清理缓存
        removeCache();
        return this.removeById(id)?1:0;
    }

    /**
     * 清理缓存
     */
    public void removeCache(){
        redisTemplate.delete(CacheConstants.NURSING_PROJECT_LIST_KEY);
    }


    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 获取全部护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> getAll() {
        //1.从缓存中获取数据
        List<String> stringList = redisTemplate.opsForList().range(CacheConstants.NURSING_PROJECT_LIST_KEY, 0, -1);
        List<NursingProjectVo> nursingProjectVoList = null;
        if(!CollectionUtils.isEmpty(stringList)){
            nursingProjectVoList = stringList.stream().map(x -> {
                return JSON.parseObject(x, NursingProjectVo.class);
            }).collect(Collectors.toList());
        }
        //2.判断数据是否为空
        if (CollectionUtils.isEmpty(nursingProjectVoList)){
            //3.为空.从数据库中查询数据
            List<NursingProject> nursingProjectList = this.lambdaQuery().eq(NursingProject::getStatus, 1).list();
            //4.判断查询的数据是否为空
            if(CollectionUtils.isEmpty(nursingProjectList)){
                return new ArrayList<>();
            }
            nursingProjectVoList = nursingProjectList.stream().map(x->{
                NursingProjectVo nursingProjectVo = new NursingProjectVo();
                nursingProjectVo.setLabel(x.getName());
                nursingProjectVo.setValue(x.getId().intValue());
                //5.将数据添加到缓存中
                redisTemplate.opsForList().leftPush(CacheConstants.NURSING_PROJECT_LIST_KEY,JSON.toJSONString(nursingProjectVo));
                return nursingProjectVo;
            }).collect(Collectors.toList());
        }
            //6.返回数据
        return nursingProjectVoList;
    }
}
